/**
 * @(#)SysVerController.java 2016年12月9日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.SysVerVO;
import org.jsbd.boss.domian.channel.SimulatorSysVer;
import org.jsbd.boss.service.ISimulatorSysVerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 系统版本配置
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/sysver")
public class SysVerController {
	public static final Logger LOGGER = LoggerFactory.getLogger(SysVerController.class);
	@Resource
	private ISimulatorSysVerService sysVerService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		return "sysver/list";
	}

	@RequestMapping(value = "list", params = "json")
	@ResponseBody
	public List<SimulatorSysVer> queryList(DataGridModel page, SysVerVO svVO) throws Exception {
		//return sysVerService.selectSimulatorSysVer(ver);
		return sysVerService.selectSimulatorSysVerByVO(svVO);
	}

	@RequestMapping(value = "{ver}/update", method = RequestMethod.GET)
	public String update(@PathVariable String ver, Model model) {
		if (StringUtils.isNotBlank(ver)) {
			SysVerVO vo = new SysVerVO();
			vo.setVer(ver);
			List<SimulatorSysVer> vers = sysVerService.selectSimulatorSysVerByVO(vo);
			if (vers != null && vers.size() > 0) {
				model.addAttribute("ver", vers.get(0));
			}
		}
		return "sysver/update";
	}

	@RequestMapping(value = "{ver}/update", method = RequestMethod.POST)
	public void update(HttpServletResponse response, SimulatorSysVer simulatorSysVer, @PathVariable String ver) {
		try {
			String ver1 = simulatorSysVer.getVer();
			if (StringUtils.isNotBlank(ver1)) {
				List<SimulatorSysVer> simVer = sysVerService.selectSimulatorSysVer(ver1);
				if (simVer != null && simVer.size() > 0) {
					if (!ver.equals(simVer.get(0).getVer())) {
						ResponseUtils.responseInfoExists(response);
					} else {
						sysVerService.updateSimulatorSysVer(simulatorSysVer);
						ResponseUtils.responseSuccess(response);
					}
				} else {
					sysVerService.updateSimulatorSysVer(simulatorSysVer);
					ResponseUtils.responseSuccess(response);
				}
			} else {
				ResponseUtils.responseFailure(response);
			}
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "{ver}/delete", method = RequestMethod.POST)
	public void del(HttpServletResponse response, SimulatorSysVer simulatorSysVer, @PathVariable String ver) {
		try {
			sysVerService.delSimulatorSysVer(ver);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "sysver/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public void add(HttpServletResponse response, SimulatorSysVer simulatorSysVer) {
		try {
			if (StringUtils.isNotBlank(simulatorSysVer.getVer())) {
				sysVerService.insertSimulatorSysVer(simulatorSysVer);
				ResponseUtils.responseSuccess(response);
			} else {
				ResponseUtils.responseFailure(response);
			}
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}


}
